package com.lw.controller;

import com.lw.common.aop.LogOperation;
import com.lw.common.enums.LogModuleEnum;
import com.lw.common.file.FileInfo;
import com.lw.common.file.FileService;
import com.lw.common.utils.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Resource;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private FileService fileService;

    /**
     * 上传文件
     *
     * @param file 表单文件对象
     * @return 文件信息
     */
    @PostMapping(value = "/uploadFile")
    @Operation(summary = "文件上传")
    @LogOperation(module = LogModuleEnum.LOG_FILE, value = "文件上传")
    public ResultVo<FileInfo> uploadFile(
            @Parameter(name = "file", description = "表单文件对象", required = true, in = ParameterIn.DEFAULT, schema = @Schema(name = "file", format = "binary"))
            @RequestPart(value = "file") MultipartFile file
    ) {
        FileInfo fileInfo = fileService.uploadFile(file);
        return ResultVo.success(fileInfo);
    }

    /**
     * 删除文件
     *
     * @param filePath 文件完整URL
     * @return 删除结果
     */
    @DeleteMapping(value = "deleteFile")
    @Operation(summary = "文件删除")
    @SneakyThrows
    public ResultVo<?> deleteFile(
            @Parameter(description = "文件路径") @RequestParam String filePath
    ) {
        boolean result = fileService.deleteFile(filePath);
        return ResultVo.success(result);
    }


    /**
     * 大上传文件
     *
     * @param file        表单文件对象
     * @param chunkNumber 分片序号
     * @param totalChunks 总分片数
     * @return 文件信息
     * 目前测试本地上传 , minio尚未测试
     */
    @PostMapping("/uploadByChunk")
    public ResultVo<String> uploadFile(@RequestParam("file") MultipartFile file,
                                       @RequestParam("chunkNumber") int chunkNumber,
                                       @RequestParam("totalChunks") int totalChunks) {
        fileService.uploadByChunk(file, chunkNumber, totalChunks);
        return ResultVo.success("文件上传成功");
    }

    /**
     * 检查某个分片是否已经上传
     *
     * @param fileName    文件名
     * @param chunkNumber 分片序号
     */
    @GetMapping("/checkChunkUploaded")
    public ResultVo<HashMap<String, Boolean>> checkChunkUploaded(@RequestParam String fileName, @RequestParam int chunkNumber) {
        HashMap<String, Boolean> uploaded = fileService.checkChunkUploaded(fileName, chunkNumber);
        return ResultVo.success(uploaded);
    }

    /**
     * 合并分片
     *
     * @param request 合并请求
     * @return 合并结果
     */
    @PostMapping("/merge")
    public ResultVo<String> mergeChunks(@RequestBody MergeRequest request) {
        String fileName = request.getFileName();
        int totalChunks = request.getTotalChunks();
        String s = fileService.mergeChunks(fileName, totalChunks);
        return ResultVo.success(s);
    }

    @Data
    public static class MergeRequest {
        private String fileName;
        private int totalChunks;
    }
}
